<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/js.cookie.js"></script>
<script type="text/javascript" src="/js/Math.uuid.js"></script>
<script type="text/javascript" src="/js/util.js"></script>
<link href="/js/intro/introjs.css" media="screen" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/intro/intro.js"></script>
<script type="text/javascript">
    var timeMark = '';
    var askIngLogin = false;
    $(function(){
        auth_util.initAuthModule();
    })
    var auth_util = {
        askLogin:function(error){
            error = error==undefined?"":error;
            //init from cookie
            //console.log(JSON.parse(util.getClientInfo()))
            var cookieUser = JSON.parse(util.getClientInfo())
            $("#userInfo").attr("data-intro",'username:<input value="'+(cookieUser.uid==undefined?'':cookieUser.uid)+'" /><br/>password:<input type=\"password" /><br/><span style="color:red;">'+error+'</span>')

            askIngLogin = true;
            introJs().setOptions({
             'doneLabel': 'Login',
             'exitOnEsc':false,
             'exitOnOverlayClick':false,
             'showStepNumbers':false,
             'scrollToElement':true,
             'keyboardNavigation':false,
             'showBullets':false,
             'tooltipClass':'intro_login'
             }).start().oncomplete(function() {
                 var inputs = $(".intro_login input");
                 var uname = $(inputs[0]).val()
                 var pwd = $(inputs[1]).val()
                 console.log(uname+"|"+pwd)
                 //introJs().refresh()
                 util.ajax("/auth/login/"+uname,function(data){
                    if(util.isSuccess(data)) {
                        auth_util.initUserInfo();
                    }
                    else {
                        auth_util.askLogin(util.getErrorMsg(data))
                    }
                 })
                 askIngLogin = false;
             });
        },
        logout:function(){
            util.ajax("/auth/logout",function(data){
                $("#userNickName").empty()
                auth_util.askLogin()
            })
        },
        initUserInfo:function(){
            var cookieUser = JSON.parse(util.getClientInfo())
            $("#userNickName").html(cookieUser.username+"<a href='javascript:{auth_util.logout()}'>logout</a>")
        },
        initAuthModule:function(){
            window.onblur = function(){
                timeMark = ""+new Date().getTime();
            }
            window.onfocus = function(){
                var now = new Date().getTime();
                var t = now-0-timeMark;
                timeMark = ""+new Date().getTime();
                //console.log(t)
                if(t>1 && !askIngLogin) {
                    util.ajax("/auth",function(data){
                        if(data.current_user== undefined || data.current_user=="null") {
                            console.log("out of session")
                            auth_util.askLogin();
                        }
                        else {
                            auth_util.initUserInfo();
                        }
                    })

                }
            }
        }
    }

</script>
<p>welcome to Iservice erp! Wish you have a nice day!</p>
<p id="userInfo" biz="" data-intro='username:<input /><br/>password:<input type="password" />'>user:<span
        id="userNickName"></span></p>